import { NextResponse } from 'next/server'

// 从CoinGecko API获取加密货币数据
export async function GET() {
  try {
    // 免费API有速率限制，实际项目中应该添加缓存和错误处理
    const response = await fetch(
      'https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=20&page=1&sparkline=false',
      { next: { revalidate: 60 } } // 每60秒重新验证数据
    );

    if (!response.ok) {
      throw new Error(`API请求失败: ${response.status}`);
    }

    const data = await response.json();
    return NextResponse.json(data);
  } catch (error) {
    console.error('获取加密货币数据失败:', error);
    return NextResponse.json(
      { error: '获取加密货币数据失败' },
      { status: 500 }
    );
  }
}
